package com.example.hxpc.guidancemanagement.activity;

import android.app.Activity;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.Nullable;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.hxpc.guidancemanagement.R;
import com.example.hxpc.guidancemanagement.adapter.NumberListAdapter;
import com.example.hxpc.guidancemanagement.api.BaseApi;
import com.example.hxpc.guidancemanagement.base.NumberInfo;
import com.example.hxpc.guidancemanagement.model.JsonBase;
import com.example.hxpc.guidancemanagement.model.JsonFeedback;
import com.example.hxpc.guidancemanagement.tools.ToastTools;
import com.example.hxpc.guidancemanagement.tools.dataTools.OkHttpTools;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by tao on 2018/3/13.
 * 团队成员列表
 */

public class NumberListActivity extends Activity implements View.OnClickListener ,BaseApi{
    private ImageView img_add , img_back;
    private TextView te_title;
    private ListView number_list;
    private NumberListAdapter adapter;
    private List<NumberInfo> number = new ArrayList<>();
    private OkHttpTools okHttpTools = new OkHttpTools();
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_number_list);
//        initData();
        initView();
    }
    //初始化控件
    public void initView() {
        okHttpTools.getAllNumber(this);
        img_add = (ImageView) findViewById(R.id.add_mark);
        img_add.setVisibility(View.GONE);
        img_back = (ImageView) findViewById(R.id.back);
        img_back.setOnClickListener(this);
        te_title = (TextView) findViewById(R.id.te_title);
        te_title.setText("成员列表");
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        number_list = (ListView) findViewById(R.id.number_list);
        adapter = new NumberListAdapter(this,number,dm.widthPixels);
        number_list.setAdapter(adapter);
    }

    //初始化数据
    public void initData(){
        NumberInfo nif = new NumberInfo();
        nif.setNumberName("涛");
        nif.setNumberPhoto("http://192.168.2.186:8080/GuidanceManagement/userHeadPhoto/1520845265737avatar.jpg");
        nif.setNumberType("普通成员");
        NumberInfo nif1 = new NumberInfo();
        nif1.setNumberName("哈哈");
        nif1.setNumberPhoto("http://192.168.2.186:8080/GuidanceManagement/userHeadPhoto/1520845265737avatar.jpg");
        nif1.setNumberType("超级用户");
        number.add(nif);
        number.add(nif1);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.back:
                finish();
                break;
        }
    }

    @Override
    public void backMsg(String success, JsonBase jsonBase) {

    }

    @Override
    public void backMsg(String success, JsonFeedback jsonFeedback) {
        if (success.equals("true")){
            number.clear();
            for (int i = 0 ; i<jsonFeedback.getData().size() ; i++){
                NumberInfo nif = new NumberInfo();
                nif.setNumberName(jsonFeedback.getData().get(i).getName());
                nif.setNumberPhoto(jsonFeedback.getData().get(i).getHeadPhoto());
                nif.setNumberTelephone(jsonFeedback.getData().get(i).getTelephone());
                if (jsonFeedback.getData().get(i).getFlg()==2){
                    nif.setNumberType("超级用户");
                }else if (jsonFeedback.getData().get(i).getFlg()==3){
                    nif.setNumberType("普通用户");
                }else if (jsonFeedback.getData().get(i).getFlg()==1){
                    nif.setNumberType("管理员");
                }
                number.add(nif);
            }
            adapter.notifyDataSetChanged();
        }else {
            ToastTools.toastMsg(this,jsonFeedback.getMsg());
        }
    }
}
